<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>arc4.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_crypt_arc4.js.html">arc4.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2005 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview ARC4 streamcipher implementation.  A description of the
<a name="line17"></a> * algorithm can be found at:
<a name="line18"></a> * http://www.mozilla.org/projects/security/pki/nss/draft-kaukonen-cipher-arcfour-03.txt.
<a name="line19"></a> *
<a name="line20"></a> * Usage:
<a name="line21"></a> * &lt;code&gt;
<a name="line22"></a> *   var arc4 = new goog.crypt.Arc4();
<a name="line23"></a> *   arc4.setKey(key);
<a name="line24"></a> *   arc4.discard(1536);
<a name="line25"></a> *   arc4.crypt(bytes);
<a name="line26"></a> * &lt;/code&gt;
<a name="line27"></a> *
<a name="line28"></a> * Note: For converting between strings and byte arrays, goog.crypt.base64 may
<a name="line29"></a> * be useful.
<a name="line30"></a> *
<a name="line31"></a> */
<a name="line32"></a>
<a name="line33"></a>goog.provide(&#39;goog.crypt.Arc4&#39;);
<a name="line34"></a>
<a name="line35"></a>goog.require(&#39;goog.asserts&#39;);
<a name="line36"></a>
<a name="line37"></a>
<a name="line38"></a>
<a name="line39"></a>/**
<a name="line40"></a> * ARC4 streamcipher implementation.
<a name="line41"></a> * @constructor
<a name="line42"></a> */
<a name="line43"></a>goog.crypt.Arc4 = function() {
<a name="line44"></a>  /**
<a name="line45"></a>   * A permutation of all 256 possible bytes.
<a name="line46"></a>   * @type {Array.&lt;number&gt;}
<a name="line47"></a>   * @private
<a name="line48"></a>   */
<a name="line49"></a>  this.state_ = [];
<a name="line50"></a>
<a name="line51"></a>  /**
<a name="line52"></a>   * 8 bit index pointer into this.state_.
<a name="line53"></a>   * @type {number}
<a name="line54"></a>   * @private
<a name="line55"></a>   */
<a name="line56"></a>  this.index1_ = 0;
<a name="line57"></a>
<a name="line58"></a>  /**
<a name="line59"></a>   * 8 bit index pointer into this.state_.
<a name="line60"></a>   * @type {number}
<a name="line61"></a>   * @private
<a name="line62"></a>   */
<a name="line63"></a>  this.index2_ = 0;
<a name="line64"></a>};
<a name="line65"></a>
<a name="line66"></a>
<a name="line67"></a>/**
<a name="line68"></a> * Initialize the cipher for use with new key.
<a name="line69"></a> * @param {Array.&lt;number&gt;} key A byte array containing the key.
<a name="line70"></a> * @param {number=} opt_length Indicates # of bytes to take from the key.
<a name="line71"></a> */
<a name="line72"></a>goog.crypt.Arc4.prototype.setKey = function(key, opt_length) {
<a name="line73"></a>  goog.asserts.assertArray(key, &#39;Key parameter must be a byte array&#39;);
<a name="line74"></a>
<a name="line75"></a>  if (!opt_length) {
<a name="line76"></a>    opt_length = key.length;
<a name="line77"></a>  }
<a name="line78"></a>
<a name="line79"></a>  var state = this.state_;
<a name="line80"></a>
<a name="line81"></a>  for (var i = 0; i &lt; 256; ++i) {
<a name="line82"></a>    state[i] = i;
<a name="line83"></a>  }
<a name="line84"></a>
<a name="line85"></a>  var j = 0;
<a name="line86"></a>  for (var i = 0; i &lt; 256; ++i) {
<a name="line87"></a>    j = (j + state[i] + key[i % opt_length]) &amp; 255;
<a name="line88"></a>
<a name="line89"></a>    var tmp = state[i];
<a name="line90"></a>    state[i] = state[j];
<a name="line91"></a>    state[j] = tmp;
<a name="line92"></a>  }
<a name="line93"></a>
<a name="line94"></a>  this.index1_ = 0;
<a name="line95"></a>  this.index2_ = 0;
<a name="line96"></a>};
<a name="line97"></a>
<a name="line98"></a>
<a name="line99"></a>/**
<a name="line100"></a> * Discards n bytes of the keystream.
<a name="line101"></a> * These days 1536 is considered a decent amount to drop to get the key state
<a name="line102"></a> * warmed-up enough for secure usage. This is not done in the constructor to
<a name="line103"></a> * preserve efficiency for use cases that do not need this.
<a name="line104"></a> * NOTE: Discard is identical to crypt without actually xoring any data. It&#39;s
<a name="line105"></a> * unfortunate to have this code duplicated, but this was done for performance
<a name="line106"></a> * reasons. Alternatives which were attempted:
<a name="line107"></a> * 1. Create a temp array of the correct length and pass it to crypt. This
<a name="line108"></a> *    works but needlessly allocates an array. But more importantly this
<a name="line109"></a> *    requires choosing an array type (Array or Uint8Array) in discard, and
<a name="line110"></a> *    choosing a different type than will be passed to crypt by the client
<a name="line111"></a> *    code hurts the javascript engines ability to optimize crypt (7x hit in
<a name="line112"></a> *    v8).
<a name="line113"></a> * 2. Make data option in crypt so discard can pass null, this has a huge
<a name="line114"></a> *    perf hit for crypt.
<a name="line115"></a> * @param {number} length Number of bytes to disregard from the stream.
<a name="line116"></a> */
<a name="line117"></a>goog.crypt.Arc4.prototype.discard = function(length) {
<a name="line118"></a>  var i = this.index1_;
<a name="line119"></a>  var j = this.index2_;
<a name="line120"></a>  var state = this.state_;
<a name="line121"></a>
<a name="line122"></a>  for (var n = 0; n &lt; length; ++n) {
<a name="line123"></a>    i = (i + 1) &amp; 255;
<a name="line124"></a>    j = (j + state[i]) &amp; 255;
<a name="line125"></a>
<a name="line126"></a>    var tmp = state[i];
<a name="line127"></a>    state[i] = state[j];
<a name="line128"></a>    state[j] = tmp;
<a name="line129"></a>  }
<a name="line130"></a>
<a name="line131"></a>  this.index1_ = i;
<a name="line132"></a>  this.index2_ = j;
<a name="line133"></a>};
<a name="line134"></a>
<a name="line135"></a>
<a name="line136"></a>/**
<a name="line137"></a> * En- or decrypt (same operation for streamciphers like ARC4)
<a name="line138"></a> * @param {Array.&lt;number&gt;|Uint8Array} data The data to be xor-ed in place.
<a name="line139"></a> * @param {number=} opt_length The number of bytes to crypt.
<a name="line140"></a> */
<a name="line141"></a>goog.crypt.Arc4.prototype.crypt = function(data, opt_length) {
<a name="line142"></a>  if (!opt_length) {
<a name="line143"></a>    opt_length = data.length;
<a name="line144"></a>  }
<a name="line145"></a>  var i = this.index1_;
<a name="line146"></a>  var j = this.index2_;
<a name="line147"></a>  var state = this.state_;
<a name="line148"></a>
<a name="line149"></a>  for (var n = 0; n &lt; opt_length; ++n) {
<a name="line150"></a>    i = (i + 1) &amp; 255;
<a name="line151"></a>    j = (j + state[i]) &amp; 255;
<a name="line152"></a>
<a name="line153"></a>    var tmp = state[i];
<a name="line154"></a>    state[i] = state[j];
<a name="line155"></a>    state[j] = tmp;
<a name="line156"></a>
<a name="line157"></a>    data[n] ^= state[(state[i] + state[j]) &amp; 255];
<a name="line158"></a>  }
<a name="line159"></a>
<a name="line160"></a>  this.index1_ = i;
<a name="line161"></a>  this.index2_ = j;
<a name="line162"></a>};
</pre>


</body>
</html>
